package com.homework

//按照数据格式创建样例类
case class UserInfo(userName:String,location:String,startTime:Int,duration:Int)
object UserLocation {

  def main(args: Array[String]): Unit = {
    //把数据传入集合中
    val userInfoList:List[UserInfo] = List(
      UserInfo("UserA","LocationA",8,60),
      UserInfo("UserA","LocationA",9,60),
      UserInfo("UserB","LocationB",10,60),
      UserInfo("UserB","LocationB",11,80)
    )

    //通过groupby对数据进行分组
    val userGroupBy = userInfoList.groupBy(user => user.userName+","+user.location)
    //根据开始时间对数据进行排序
    val userOrderBy = userGroupBy.mapValues(_.sortBy(_.startTime))

    var firstTime = 0

    //对分组排序后的数据进行统计，分组数据头一行的starttime就是firsttime，再将duration相加取得总时长
    val totalUser = userOrderBy.mapValues(t=>{
      firstTime = t.head.startTime
      t.map(_.duration).sum
    })

    totalUser.foreach{
      case (k, v) => println(s"$k,$firstTime,$v")
    }
  }
}
